﻿using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Fieldses
{
    public class CreateFieldHandler : IRequestHandler<CreateFieldCommand, Result>
    {
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> _bedroomRepository;

        /// <summary>
        /// 栏位仓储
        /// </summary>
        private readonly IBaseRepository<Fields> _feildsRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<CreateFieldHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bedroomRepository"></param>
        /// <param name="feildsRepository"></param>
        /// <param name="logger"></param>
        public CreateFieldHandler(IBaseRepository<Bedrooms> bedroomRepository, IBaseRepository<Fields> feildsRepository, ILogger<CreateFieldHandler> logger)
        {
            _bedroomRepository = bedroomRepository;
            _feildsRepository = feildsRepository;
            _logger = logger;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(CreateFieldCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var res = 0;
                if (request.IsYes == 1)
                {
                    //添加栏位
                    res = await _feildsRepository.Add(new Fields()
                    {
                        FieldName = request.FieldName,
                        BedroomId = request.BedroomId,
                    });
                }
                else if (request.IsYes == 2)
                {
                    //批量添加栏位
                    var feildList = new List<Fields>();
                    for (var i = 0; i < request.NumberofFields; i++)
                    {
                        var feild = new Fields();
                        var num = request.startNumber + i;
                        request.FieldName = request.prefix + num;
                        feild.FieldName = request.FieldName;
                        feild.BedroomId = request.BedroomId;

                        feildList.Add(feild);
                    }
                    res = _feildsRepository.AddRange(feildList);
                }
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加栋舍栏位成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "添加栋舍栏位失败" + ex.Message;
                _logger.LogError("添加栋舍栏位异常" + ex.Message);
            }
            return result;
        }
    }
}
